package com.peas.platform.module.icims;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peas.platform.module.icims.base.sys.dto.RecycleDto;
import com.peas.platform.module.icims.base.sys.service.RecycleService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RecycleSelectTestCase {

    @Resource
    private RecycleService recycleService;

    private List<Long> tableIds(int size) {
        Page<RecycleDto> pager = new Page<>();
        pager.setCurrent(1);
        pager.setSize(size);
        Page<RecycleDto> page = recycleService.page(pager);
        List<Long> ids = new ArrayList<>();
        page.getRecords().forEach(dto -> ids.add(dto.getTableId()));
        return ids;
    }

    @Test
    public void selectAsync() {
        List<Integer> splitSizes = Arrays.asList(1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 50);
        List<Long> tableIds = tableIds(1000);
        for (int splitSize : splitSizes) {
            long time = selectAsync(tableIds, splitSize);
            System.out.println("splitSize:" + splitSize + ",used:" + time);
        }
    }

    @Test
    public void select() {
        List<Integer> size = Arrays.asList(1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 50);
        List<Long> list1 = new ArrayList<>();
        for (int s : size) {
            List<Long> tableIds = tableIds(s);
            list1.add(select(tableIds));
        }
        System.out.println(list1);
    }

    private long select(List<Long> tableIds) {
        StopWatch sw = new StopWatch();
        sw.start();
        recycleService.listByField("tableId", tableIds);
        sw.stop();
        return sw.getLastTaskTimeMillis();
    }

    private long selectAsync(List<Long> tableIds, int splitSize) {
        StopWatch sw = new StopWatch();
        sw.start();
        recycleService.listByFieldAsync("tableId", tableIds, splitSize);
        sw.stop();
        return sw.getLastTaskTimeMillis();
    }

}
